package de.yourdomian.managedbean;

import java.util.ArrayList;
import java.util.List;

import de.yourdomian.entity.AbstractBaseEntity;

public class SelectableEntity<ENTITY extends AbstractBaseEntity> {

    ENTITY entity;

    boolean selected;

    public SelectableEntity(ENTITY entity, boolean selected) {
        this.entity = entity;
        this.selected = selected;
    }

    public ENTITY getEntity() {
        return entity;
    }

    public void setEntity(ENTITY entity) {
        this.entity = entity;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public static <ENTITY extends AbstractBaseEntity> boolean isEntityInEntityList(
        List<ENTITY> entityList,
        ENTITY entity) {

        return entityList.contains(entity);
    }

    public static <ENTITY extends AbstractBaseEntity> List<ENTITY> getSelectedEntitiesAsList(

        List<SelectableEntity<ENTITY>> selectedableEntityList) {

        List<ENTITY> selectedEntities = new ArrayList<ENTITY>();

        for(SelectableEntity<ENTITY> selectedSelectableEntity : selectedableEntityList) {
            if(selectedSelectableEntity.isSelected()) {
                selectedEntities.add(selectedSelectableEntity.getEntity());
            }
        }
        return selectedEntities;
    }

    public static <ENTITY extends AbstractBaseEntity> void updateSelectedEntities(
        List<SelectableEntity<ENTITY>> selectableEntityList,
        List<ENTITY> entityList) {

        for(SelectableEntity<ENTITY> selectedEntity : selectableEntityList) {
            selectedEntity.setSelected(isEntityInEntityList(entityList, selectedEntity.getEntity()));
        }
    }

    public static <ENTITY extends AbstractBaseEntity> List<SelectableEntity<ENTITY>> createSelectableEntityList(
        List<ENTITY> entityList) {

        List<SelectableEntity<ENTITY>> selectableEntityList = new ArrayList<SelectableEntity<ENTITY>>();

        for(ENTITY entity : entityList) {
            SelectableEntity<ENTITY> selectableEntity = new SelectableEntity<ENTITY>(entity, false);
            selectableEntityList.add(selectableEntity);
        }
        return selectableEntityList;
    }

    public static <ENTITY extends AbstractBaseEntity> SelectableEntity<ENTITY> findSelectedEntityWithEntityInSelectedEntityList(
        ENTITY entity,
        List<SelectableEntity<ENTITY>> selectableEntityList) {

        for(SelectableEntity<ENTITY> selectableEntity : selectableEntityList) {
            if(entity.equals(selectableEntity.getEntity())) {
                return selectableEntity;
            }
        }
        return null;
    }

    public static <ENTITY extends AbstractBaseEntity> void setAllEntitiesInSelectableEntityListTo(
        List<SelectableEntity<ENTITY>> selectableEntityList,
        boolean selected) {

        for(SelectableEntity<ENTITY> selectableEntity : selectableEntityList) {
            selectableEntity.setSelected(selected);
        }
    }
}